#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Group Project on Word Alignment

David Coles, Guan Gui, Kai Chen; 2010
$LastChangedBy: guiguan $
"""

__version__ = "$Rev: 133 $"

from load import load
from textwrap import fill
from AlignedSent import AlignedSent
from AlignedSent import gen_dict

def demo():
    """This is use-cases registry
    """
    aligned_sents = load('../../data/giza/alignment-en-fr', 'latin_1')
    use_case_1(aligned_sents)
    print
    use_case_2(1, 1, aligned_sents)
    print
    use_case_3(aligned_sents)
    print
    use_case_4(aligned_sents)
    print
    use_case_5(aligned_sents)
    print
    use_case_6(aligned_sents)
    print
    use_case_7()
    print
    use_case_8(aligned_sents)

def use_case_1(aligned_sents):
    """Find French word(s) aligned with nth English word, and find English
    word(s) aligned with nth French word 
    """
    aligned_sent = aligned_sents[-1]
    print '[use case 1.1]'
    print fill('Find French word(s) aligned with 4th last English word "%s" in \
sentence "%s"' % (aligned_sent.sent1[-4], ' '.join(aligned_sent.sent1)), 80)
    print '(result): %s' % (', '.join(
        aligned_sent.get_sent1_nth0_aligned_words(len(aligned_sent.sent1) - 4)))

    print

    print '[use case 1.2]'
    print fill('Find English word(s) aligned with 9th French word "%s" in the \
same sentence as in use case 1.1' % (aligned_sent.sent2[8]), 80)
    print '(result): %s' % (', '.join(
        aligned_sent.get_sent2_nth1_aligned_words(9)))

def use_case_2(i, j, aligned_sents):
    """Find the expected number of times that the word in position j of French
    is connected to the word in position i of English
    """
    print '[use case 2]'
    print fill('Find the expected number of times that the word in position %d \
of French is connected to the word in position %d of English' % (j, i), 80)
    count = 0
    valid_count = 0
    for aligned_sent in aligned_sents:
        try:
            if j in aligned_sent.sent1_index[i]:
                valid_count += 1
        except IndexError:
            continue
        else:
            count += 1
    print '(result): %d / %d (%.2f%%)' % (valid_count, count,
            float(valid_count) / count * 100 )


def use_case_3(aligned_sents):
    """Dump the first 3 aligned sentences out in GIZA format
    """
    print "[use case 3]"
    for aligned_sent in aligned_sents[:3]:
        print repr(aligned_sent)


def use_case_4(aligned_sents):
    """Return the sets of word aligned with the English words in the second 
    sentence.
    """
    print "[use case 4]"
    print "Sets of words aligned with the English word 'I':"
    print [sent[1] for sent in aligned_sents[1].sent1_sets() if
            sent[0]=="I"]
    print "Sets of words aligned with the English word 'you':"
    print [sent[1] for sent in aligned_sents[1].sent1_sets() if
            sent[0]=="you"]
    print "Sets of words aligned with the English word 'Parliament':"
    print [sent[1] for sent in aligned_sents[1].sent1_sets() if
            sent[0]=="Parliament"]


def use_case_5(aligned_sents):
    """Return the sets of word aligned with the French words in the second 
    sentence.
    """
    print "[use case 5]"
    print "Sets of words aligned with the French word 'bonnes':"
    print [sent[1] for sent in aligned_sents[1].sent2_sets() if
            sent[0]=="bonnes"]
    print "Sets of words aligned with the French word 'le':"
    print [sent[1] for sent in aligned_sents[1].sent2_sets() if
            sent[0]=="le"]
    print "Set flatten_dialigned with the French word 'espérant':"
    print [sent[1] for sent in aligned_sents[1].sent2_sets() if
            sent[0]==u"espérant"]

def use_case_6(aligned_sents):
    """Use the words alignments as Fr <-> En dictionary to query word.
    """
    print "[use case 6]"
    print "Translate French word 'le' into English"
    (en_fr, fr_en) = gen_dict(aligned_sents)
    print fr_en['le']
    print "Translate English word 'something' into French"
    print en_fr['something']

def use_case_7():
    """Store optional language information in AlignedSent
    """
    print "[use case 7]"
    print "Store optional language information in AlignedSent"
    words = ['word1', 'word2', 'word3', 'word4']
    mots = ['mot1', 'mot2', 'mot3', 'mot4', 'mot5']
    alignments = [(0, 0), (1, 0), (2, 1), (3, 2), (3, 4)]
    aligned_sent = AlignedSent(words, mots, alignments)
    aligned_sent.sent1_lang = 'en'
    aligned_sent.sent2_lang = 'fr'
    print "sentence 1 language: \
aligned_sent.sent1_lang = %s" % (aligned_sent.sent1_lang)
    print "sentence 2 language: \
aligned_sent.sent2_lang = %s" % (aligned_sent.sent2_lang)

def use_case_8(aligned_sents):
    """Visualize aligned sentences
    """
    print "[use case 8]"
    print "Visualize the alignments"
    for aligned_sent in aligned_sents[4:7] + aligned_sents[1:2]:
        aligned_sent.gui_repr()
    AlignedSent.block_and_view_gui_repr()

if __name__ == "__main__":
    demo()
